package cn.lizemin.controller;

import cn.lizemin.entity.Employee;
import cn.lizemin.service.EmployeeService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @author lzm
 * @version 1.0
 * @description: TODO
 * @date 2022/1/2 15:55
 */
@Controller
@RequestMapping("/employee")
public class EmployeeController {

    private static final Logger log = LoggerFactory.getLogger(EmployeeController.class);
    private EmployeeService employeeService;

    @Autowired
    public EmployeeController(EmployeeService employeeService) {
        this.employeeService = employeeService;
    }

    /**
     * 根据id删除员工信息
     * @param id
     * @return
     */
    @RequestMapping("delete")
    public String deleteEmployee(Integer id) {
        log.debug("删除的id：{}",id);
//        1.根据id删除员工信息
        employeeService.delete(id);
//        2.跳转到员工列表页面
        return "redirect:/employee/list";
    }

    /**
     * 更新员工信息
     * @param employee
     * @return
     */
    @RequestMapping("update")
    public String updateEmployee(Employee employee) {
        log.debug("员工id：{}",employee.getId());
        log.debug("员工名称：{}",employee.getName());
        log.debug("员工工资：{}",employee.getSalary());
        log.debug("员工生日：{}",employee.getBirthday());
        log.debug("员工性别：{}",employee.getGender());
//        1.更新员工信息
        employeeService.update(employee);
//        2.跳转到员工列表
        return "redirect:/employee/list";
    }

    @RequestMapping("detail")
    public String detailEmployee(Integer id,Model model) {
        log.debug("接收id: {}",id);
//        1.通过id查询一个员工
        Employee employee = employeeService.findEmployeeById(id);
//        2.存入作用域
        model.addAttribute("employee", employee);

        return "updateEmp";//跳转页面
    }

    /**
     * 添加员工
     * @param:  员工
     * @return: 重定向到员工列表
     * @date: 2022/1/3 9:35
     */
    @RequestMapping("add")
    public String addEmployee(Employee employee) {
        log.debug("员工名称：{}",employee.getName());
        log.debug("员工工资：{}",employee.getSalary());
        log.debug("员工生日：{}",employee.getBirthday());
        log.debug("员工性别：{}",employee.getGender());
//        1.保存员工信息
        employeeService.addEmployee(employee);
        return "redirect:/employee/list";
    }

    /**
     * 员工列表
     * @return
     */
    @RequestMapping("list")
    public String listEmployee(HttpServletRequest request, Model model) {
//        获取员工列表
        List<Employee> employees = employeeService.list();
//        request.setAttribute("employee",employees);
        model.addAttribute("employees", employees);

        return "emplist";
    }



}
